import axios from "axios";

//设置超时时间
axios.defaults.timeout = 12 * 1000; //by guo 2020-3-18-20:25修改 请求超时时间

var baseURL = "http://47.74.19.72:7009/"; //日本正式环境  by 2020-2-23 10:31
// var baseURL = "http://47.74.3.86:7009/"; //日本测试环境  by 2020-2-23 10:31

//websocket 变量声明和导出
var WebsocketServerUrl = {
  webSocketServer: "ws://47.74.19.72:7050/websocket" //日本测试环境
  // webSocketServer: "ws://47.74.3.86:7050/websocket" //日本测试环境2
};
/*---------记得修改三个 -baseURL -webSocketServer-MQTT_SERVICE------*/
const MQTT_SERVICE = "ws://47.74.19.72:18080/mqtt"; //by guo 2020-2-20 15:34添加  远程办公mqtt  日本测试环境
// const MQTT_SERVICE = "ws://47.74.3.86:18080/mqtt"; //by guo 2020-2-20 15:34添加  远程办公mqtt  日本测试环境2

import { Message } from "element-ui";
import Qs from "qs";

// 解决axios发送预请求问题，默认是非简单请求需要变为简单请求
axios.defaults.headers["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";

axios.defaults.headers.Authorization  = "1_516194bf34054e1a92579c2e458056b6";


export { WebsocketServerUrl };
export { baseURL };


const MQTT_USERNAME = "admin";
const MQTT_PASSWORD = "password";
const MQTT_CLIENTID = "mqtitId-" + new Date().getTime();

export { MQTT_SERVICE, MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENTID };

/**
 * 封装get方法
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function get(url, params = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .get(baseURL + url, {
        params: params,
        headers: {
          "Accept-Language": acceptLanguage,
          Authorization: window.sessionStorage.getItem("Authorization")
        }
      })
      .then(response => {
        if (response.data.code == 200) {
          resolve(response.data.result);
        } else if (response.data.code == 100010) {
          //by guo 2019-11-30-13:07添加
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        reject(error);
        Message.error({ message: error.response.data.message });
      });
  });
}

/**
 * by guo 2019-12-26-14:50添加
 * 封装put请求 需要传三个参数 url data type
 * @param url 路径
 * @param data 请求参数
 * @param type 类型 'application/x-www-form-urlencoded' 或者 'application/json' 根据后端请求content-type类型决定
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function put(url, data = {}, type, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  // console.log(acceptLanguage, "acceptLanguage-------");
  return new Promise((resolve, reject) => {
    axios({
      method: "put",
      url: baseURL + url,
      headers: {
        "Accept-Language": acceptLanguage,
        "Content-type": type,
        Authorization: window.sessionStorage.getItem("Authorization")
      },
      params: data
    })
      .then(response => {
        // console.log(response);
        if (response.status == 200) {
          if (response.data.status || response.data.code == 200) {
            resolve(response.data);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              //Message.error({message:"请重新登录"});
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            resolve(response.data);
          }
        } else {
          // resolve(response.data);
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        console.log(error);
        if (error.response.status == 401) {
          sessionStorage.removeItem("user");
        } else {
          reject(error);
          Message.error({ message: error.response.data.message });
        }
      });
  });
}

/**
 * 封装delete方法
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function deleteList(url, params = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .delete(baseURL + url, {
        params: params,
        headers: {
          "Accept-Language": acceptLanguage,
          Authorization: window.sessionStorage.getItem("Authorization")
        }
      })
      .then(response => {
        if (response.data.code == 200) {
          resolve(response.data);
        } else if (response.data.code == 100010) {
          //by guo 2019-11-30-13:07添加
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          Message.error({ message: response.data.message });
        }
      })
      .catch(error => {
        reject(error);
        Message.error({ message: error.response.data.message });
      });
  });
}

//封装post请求,
//可以不用传递token ,不传则使用默认的token
//@param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
export function post(url, paramData, token, type, acceptLanguage) {
  if (token == null || token == "") {
    token = window.sessionStorage.getItem("Authorization");
  }
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  if (type != null && type == "json") {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: baseURL + url,
        headers: {
          "Accept-Language": acceptLanguage,
          "Content-type": "application/json",
          Authorization: token
        },
        params: {},
        data: paramData //不要序列化
      })
        .then(response => {
          if (response.data.code == 200) {
            resolve(response.data.result);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              Message.error({ message: "请重新登录" });
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            reject(response.data);
            // Message.error({message:"请求异常"});
          }
        })
        .catch(error => {
          console.log(error.response);
          if (error.response.status == "401") {
            sessionStorage.removeItem("user");
            this.$store.commit("logout");
            this.$router.push({ path: "/login" });
          } else {
            if (error.response.data.message) {
              Message.error({ message: error.response.data.message });
            } else {
              Message.error({ message: "服务器异常" });
            }
          }
        });
    });
  } else {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: baseURL + url,
        headers: {
          "Accept-Language": acceptLanguage,
          "Content-type": "application/x-www-form-urlencoded",
          Authorization: token
        },
        params: paramData
      })
        .then(response => {
          if (response.data.code == 200) {
            resolve(response.data.result);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              //Message.error({message:"请重新登录"});
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            reject(response.data);
            // Message.error({message:"请求异常"});
          }
        })
        .catch(error => {
          console.log(error.response);
          if (error.response.status == "401") {
            sessionStorage.removeItem("user");
            this.$store.commit("logout");
            this.$router.push({ path: "/login" });
          } else {
            if (error.response.data.message) {
              Message.error({ message: error.response.data.message });
            } else {
              Message.error({ message: "服务器异常" });
            }
          }
        });
    });
  }
}

/**
 * 封装getToken方法
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function getToken(url, params = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .get(baseURL + url, {
        params: params,
        headers: {
          "Accept-Language": acceptLanguage,
          Authorization: "Basic Y21ueS1jbGllbnQtdWFjOmNtbnlDbGllbnRTZWNyZXQ="
        }
      })
      .then(response => {
        if (response.data.code == 200) {
          resolve(response.data.result);
        } else if (response.data.code == 100010) {
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        reject(error);
        Message.error({ message: error.response.data.message });
      });
  });
}

/**
 * 封装postLogin方法
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function postLogin(url, data = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url: baseURL + url,
      headers: {
        "Accept-Language": acceptLanguage,
        "Content-type": "application/x-www-form-urlencoded",
        Authorization: "Basic Y21ueS1jbGllbnQtdWFjOmNtbnlDbGllbnRTZWNyZXQ="
      },
      params: data
    })
      .then(response => {
        // console.log(response);
        if (response.data.code == 200) {
          resolve(response.data.result);
        } else if (response.data.code == 100010) {
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          reject(response.data);
          // Message.error({ message: "请求异常" });
        }
      })
      .catch(err => {
        console.log(err.response);
        reject(err);
        if (err.response.data.message) {
          Message.error({ message: err.response.data.message });
        } else {
          Message.error({ message: "请重新登录" });
        }
      });
  });
}
/**
 * 封装post请求 form表单请求
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */

export function postForm(url, data = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url: baseURL + url,
      headers: {
        "Accept-Language": acceptLanguage,
        "Content-type": "application/x-www-form-urlencoded",
        Authorization: window.sessionStorage.getItem("Authorization")
      },
      params: data
    })
      .then(response => {
        // console.log(response);
        if (response.status == 200) {
          if (response.data.status || response.data.code == 200) {
            resolve(response.data);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              //Message.error({message:"请重新登录"});
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            resolve(response.data);
          }
        } else {
          // resolve(response.data);
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        console.log(error);
        if (typeof error.response !== "undefined") {
          if (error.response.status == 401) {
            sessionStorage.removeItem("user");
          } else {
            reject(error);
            Message.error({ message: error.response.data.message });
          }
        } else {
          reject(error);
        }
      });
  });
}

/**
 * 封装post请求 上传闹钟铃声请求
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */

export function postMyForm(url, data, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .post(baseURL + url, Qs.stringify(data), {
        headers: {
          "Accept-Language": acceptLanguage,
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
          Authorization: window.sessionStorage.getItem("Authorization")
        }
      })
      .then(response => {
        if (response.status == 200) {
          if (response.data.status || response.data.code == 200) {
            resolve(response.data);
          } else {
            resolve(response.data);
          }
        } else if (response.data.code == 100010) {
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        if (error.response.status == 401) {
          sessionStorage.removeItem("user");
          this.$router.push({ path: '/login' });
        } else {
          reject(error);
          Message.error({ message: error.response.data.message });
        }
      });
  });
}

/**
 * post json 字符串
 * 传入的是对象,然后封装为json字符串
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise<any>}
 */
export function postJson(url, data = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .post(baseURL + url, JSON.stringify(data), {
        headers: {
          "Accept-Language": acceptLanguage,
          "Content-Type": "application/json",
          Authorization: window.sessionStorage.getItem("Authorization")
        }
      })
      .then(response => {
        // console.log(response);
        if (response.status == 200) {
          if (response.data.code == 200 || response.data.status) {
            // 有些是code有些是status标记
            resolve(response.data);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              //Message.error({message:"请重新登录"});
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            resolve(response.data);
            //Message.error({message:response.data.message});
          }
        } else {
          // resolve(response.data);
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        console.log(error);
        if (error.response && error.response.status == 401) {
          sessionStorage.removeItem("user");
          this.$router.push({ path: '/login' });
        } else if (error.data == undefined) {
          reject(error);
        } else {
          reject(error);
          Message.error({ message: error.response.data.message });
        }
      });
  });
}

/**
 * 封装post请求 上传闹钟铃声请求
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */

export function postFormData(url, data = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios
      .post(baseURL + url, data, {
        headers: {
          "Accept-Language": acceptLanguage,
          "Content-Type": "multipart/form-data",
          Authorization: window.sessionStorage.getItem("Authorization")
        }
      })
      .then(response => {
        // console.log(response);
        if (response.status == 200) {
          if (response.data.status || response.data.code == 200) {
            resolve(response.data);
          } else {
            resolve(response.data);
          }
        } else if (response.data.code == 100010) {
          setTimeout(() => {
            //Message.error({message:"请重新登录"});
            this.$router.push({ path: "/login" });
            return;
          }, 500);
        } else {
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        if (error.response.status == 401) {
          sessionStorage.removeItem("user");
          this.$router.push({ path: '/login' });
        } else {
          reject(error);
          Message.error({ message: error.response.data.message });
        }
      });
  });
}

/**
 * 封装get请求 form表单请求
 * @param url
 * @param data
 * @param acceptLanguage //接收的语言 zh_CN中文   ja_JP日文  life集团需要使用国际化请求的就需要传参为ja_JP  否则就默认为中文
 * @returns {Promise}
 */
export function getForm(url, data = {}, acceptLanguage) {
  acceptLanguage =
    acceptLanguage == "" ||
    acceptLanguage == null ||
    acceptLanguage == undefined
      ? "zh_CN"
      : acceptLanguage;
  return new Promise((resolve, reject) => {
    axios({
      method: "get",
      url: baseURL + url,
      headers: {
        "Accept-Language": acceptLanguage,
        "Content-type": "application/x-www-form-urlencoded",
        Authorization: window.sessionStorage.getItem("Authorization")
      },
      params: data
    })
      .then(response => {
        // console.log(response);
        if (response.status == 200) {
          if (response.data.status || response.data.code == 200) {
            resolve(response.data);
          } else if (response.data.code == 100010) {
            setTimeout(() => {
              //Message.error({message:"请重新登录"});
              this.$router.push({ path: "/login" });
              return;
            }, 500);
          } else {
            resolve(response.data);
          }
        } else {
          // resolve(response.data);
          reject("请求异常");
          Message.error({ message: "请求异常" });
        }
      })
      .catch(error => {
        console.log(error);
        if (error.response.status == 401) {
          sessionStorage.removeItem("user");
        } else {
          reject(error);
          Message.error({ message: error.response.data.message });
        }
      });
  });
}
